package com.netty;

import java.io.Serializable;
import java.nio.ByteBuffer;

/**
 * @author zhangyingxuan
 */
public class UserInfo implements Serializable {
    private static final long serialVersionUID = -2677438549093007876L;

    private String userName;

    private int userID;

    public UserInfo buildUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public UserInfo buildUserID(int userID) {
        this.userID = userID;
        return this;
    }

    public final String getUserName() {
        return userName;
    }

    public final void setUserName(String userName) {
        this.userName = userName;
    }

    public final int getUserID() {
        return userID;
    }

    public final void setUserID(int userID) {
        this.userID = userID;
    }

    public byte[] codec() {
        ByteBuffer buf = ByteBuffer.allocate(1024);
        byte[] value = this.userName.getBytes();
        buf.putInt(value.length);
        buf.put(value);
        buf.putInt(this.userID);
        buf.flip();
        value = null;
        byte[] result = new byte[buf.remaining()];
        buf.get(result);

        return result;
    }

    public byte[] codec(ByteBuffer buffer) {
        buffer.clear();
        byte[] value = this.userName.getBytes();
        buffer.putInt(this.userName.length());
        buffer.put(value);
        buffer.putInt(this.userID);
        buffer.flip();
        value = null;
        byte[] result = new byte[buffer.remaining()];
        buffer.get(result);

        return result;
    }
}
